package com.app.core.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.widget.TextView;

import com.yanzhenjie.nohttp.Logger;

import java.util.List;

/**
 * Created by Administrator on 2017/8/8 0008.
 */

public class MarqueeTextView extends AppCompatTextView{

    private Paint mPaint;
    private static float mStep = 0f;//文字的横坐标
    //    private boolean isScroll = false;
    private static int level;
    private List<String> mStrList;

    public MarqueeTextView(Context context) {
        super(context);
    }

    public MarqueeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MarqueeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }



    public void init(List<String> strList) {
        level = 0;
        mPaint = getPaint();
        mPaint.setColor(Color.parseColor("#34a131"));
        mStrList = strList;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mStrList==null){
            return;
        }

        if (mStep > mPaint.measureText(mStrList.get(level)) + getMeasuredWidth()) {
            if (level >= mStrList.size() - 1) {
                level = 0;
            } else {
                level++;
            }
            mStep = 0;
        }
        Paint.FontMetricsInt fontMetrics = mPaint.getFontMetricsInt();
        int baseline = (getMeasuredHeight() - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
        canvas.drawText(mStrList.get(level), getMeasuredWidth() - mStep, baseline, mPaint);
        postDelayed(scrollRunable, 10);
    }


    private Runnable scrollRunable = new Runnable() {
        @Override
        public void run() {
            mStep += 1;
            invalidate();
        }
    };


}